//
//  CMDBRegisterManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/5.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBRegisterManager: NSObject {
    static let getInstance: CMDBRegisterManager = CMDBRegisterManager()
    
    private override init() {
        
    }
}
/*
 
 let mutableNames = NSMutableArray() 可变数组可以删除对象
 
 mutableNames.removeAll()
 
 mutableNames.removeObject()
 
 let regParam = CMDBRegisterParam()
 
 regParam.phone = "18612598269"
 
 regParam.vcode = ""
 
 regParam.inviteCode = "123123123"
 
 regParam.name = "王磊"
 
 regParam.useTempNames += ["三石","ThreeStone"]
 
 regParam.birthday = "1231111111"
 
 regParam.email = "yuanxingfu1314@163.com"
 
 regParam.genderType = .Mail
 
 regParam.pwd = "123456"
 
 regParam.submitPwd = "123456"
 
 printLog(regParam.useNames)
 
 do {
 
 try CMDBRegisterManager.getInstance.reg(regParam, handler: { (account) in
 
 
 
 }, error: { (rescode) in
 
 }, faliure: {
 
 })
 
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid) {
 
 
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid) {
 
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.NewPasswordInvalid) {
 
 
 } catch {
 
 
 }
 */
extension CMDBRegisterManager: CMDBRegisterManagerDelegate {
    
    func reg(regParam: CMDBRegisterParam, handler: CMDBAccountHandler, error: CMDBHttpErrorHandler, faliure: CMDBHttpFailureHandler) throws {
        
        guard regParam.phone.validPhoneNumber() else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PhoneInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid)
        }
        guard regParam.vcode.cmdb_length == VCode_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.VCodeInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.VCodeInvalid)
        }
        guard regParam.inviteCode.cmdb_length == InviteCode_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.InviteCodeInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.InviteCodeInvalid)
        }
        guard !regParam.name.isEmpty else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.NameInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.NameInvalid)
        }
        
        if !regParam.email.isEmpty && !regParam.email.validEmail() {
            
            printLog("\(CMDBUserControllerExceptionCodeType.EmailInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.EmailInvalid)
            
        }
        
        guard !regParam.trade.isEmpty else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.TradeInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.TradeInvalid)
        }
        guard regParam.pwd.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        guard regParam.submitPwd.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.NewPasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.NewPasswordInvalid)
        }
        guard regParam.pwd == regParam.submitPwd else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd)
        }
        
        guard !regParam.birthday.isEmpty else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.BirthdayInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.BirthdayInvalid)
        }
        
        guard regParam.genderType != CMDBGenderType.UnKnown else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.GenderInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.GenderInvalid)
        }
        let param = regParam.keyValues
        
        printLog(regParam.URLString())
        
        CMDBHttpManager.getInstance.request(regParam.URLString(), param: param, success: { (sysTime, data, page) in
            
            let account: CMDBAccountBean = CMDBAccountBean.objectWithKeyValues(data as! [String: AnyObject]) as! CMDBAccountBean
            
            CMDBApplicationManager.getInstance.saveAccount(account)
            
            handler(account: account)
            
            }, error: error, failure: faliure)
    }
}